/*
    BibleNotebook - A bible reader software
    Copyright (C) 2014  Williams Fernando Lopez

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
 */

package com.hexidec.ekit.component;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import wl.ui.ButtonLabel;

/**
 *
 * @author Williams Lopez
 */
public class CustomComboBox extends ButtonLabel{

    Action action;
    JPopupMenu popUpComponent;
    JList listComponent;
    ListModel listModel = new DefaultListModel();
    JScrollPane scrollPane;
    List<ActionListener> actionListenerList = new ArrayList<ActionListener>();
    Object selectedValue = null;
    boolean listNoAction = false;
    boolean changeButtonText = false;

    public CustomComboBox(Action _action){
        this.action = _action;
        popUpComponent = new JPopupMenu();
        listComponent = new JList();
        scrollPane = new JScrollPane();

        super.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                popUpComponent.pack();
                listNoAction = true;
                listComponent.setSelectedValue(selectedValue, true);
                listNoAction = false;
                popUpComponent.show(CustomComboBox.this, 0, CustomComboBox.this.getHeight());
            }
        });

        scrollPane.setMaximumSize(new Dimension(400, 250));
        scrollPane.setViewportView(listComponent);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        popUpComponent.add(scrollPane);
        listComponent.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {

                if(listNoAction){
                    return;
                }

                ActionEvent ae = new ActionEvent(listComponent.getSelectedValue(), 0, listComponent.getSelectedValue().toString());
                fireActionListenerList(ae);
                action.actionPerformed(ae);
            }
        });
        scrollPane.setMaximumSize(new Dimension(400, 250));
        scrollPane.setMinimumSize(new Dimension(75,25));
        popUpComponent.setMaximumSize(scrollPane.getMaximumSize());
        popUpComponent.setMinimumSize(scrollPane.getMinimumSize());
        listComponent.setMaximumSize(scrollPane.getMaximumSize());
        listComponent.setMinimumSize(scrollPane.getMinimumSize());

        popUpComponent.setBorder(BorderFactory.createEmptyBorder());

    }

    @Override
    public void addActionListener(ActionListener l) {
        actionListenerList.add(l);
    }

    protected void fireActionListenerList(ActionEvent ae){
        for(ActionListener al : actionListenerList){
            al.actionPerformed(ae);
        }
    }

    public void setListModel(ListModel model){
        this.listModel = model;
        listComponent.setModel(model);
    }

    public ListModel getListModel(){
        return this.listModel;
    }

    public Object getSelectedValue(){
        return selectedValue;
    }

    public void setSelectedValue(Object value){
        this.selectedValue = value;
        if(changeButtonText){
            this.setText(value.toString());
        }
    }


    @Override
    public boolean isFocusable() {
        return false;
    }

    public boolean isChangeButtonText() {
        return changeButtonText;
    }

    public void setChangeButtonText(boolean changeButtonText) {
        this.changeButtonText = changeButtonText;
    }


}
